package com.kit.data.kitopeso.net;

import android.content.Context;

import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.SpUtils;


public class ConvertUrl {

    public static void convertUrl(Context context, String json) {
        if (json.contains(",")) {
            String[] strs = json.split(",");
            if (strs.length > 0) {
                U.URL_REGION = strs[0];
                if (strs.length >= 2) {
                    U.URL_MOBILE_CODE = strs[1];
                }
                if (strs.length >= 3) {
                    U.URL_COUNTRY = strs[2];
                }
                if (strs.length >= 4) {
                    U.URL_SUBDOMAIN = strs[3];
                }
                SpUtils.save_string(context, SpUtils.KEY_TIME, json);
                boolean isDomain = isDomain(U.urlDomain);
                if (isDomain) {
                    String[] strs1 = U.urlDomain.split("\\.");
                    StringBuilder urlBuffer = new StringBuilder();
                    urlBuffer.append(U.URL_SUBDOMAIN);
                    for (int i = 1; i < strs1.length; i++) {
                        urlBuffer.append(".").append(strs1[i]);
                    }
                    U.urlDomain = urlBuffer.toString();
                }
                U.commonUrl = U.baseUrl + "/" + U.URL_REGION + "/" + U.commonPath;
                U.uploadUrl = U.baseUrl + "/" + U.URL_REGION + "/" + U.uploadPath;
                LogUtil.e("me","URL.commonUrl->"+U.commonUrl);
                LogUtil.e("me","URL.uploadUrl->"+U.uploadUrl);
            }
        }
    }

    public static boolean isDomain(String input) {
        String domainPattern = "^[a-zA-Z0-9]+([-_\\.]{1}[a-zA-Z0-9]+)*\\.[a-zA-Z]{2,5}$";
        return input.matches(domainPattern);
    }

}
